/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/jquery.jqGrid.src.js";

jQuery(function () {
    App.init();
    jQuery('.logOut').attr('href','../logins/login.html');
    var projId = getArgs().id;
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }
    var currTyp = {};    // 币种  key:value

// 获取项目基本信息
    jQuery.ajax({
        url: HOST_ADDR + '/cpmp/bidProjOnMngForPchs/view/' + projId,
        type: 'get',
        dataType: 'json',
        contentType: "application/json",
        headers: {
            'access_token': token, 'login_type': 'ums'
        },
        success: function (resp) {
            if (!resp.success) {
                alert(resp.msgCod + ' ' + resp.msgInf);
                return;
            } else {
                $(".projNam span").html(resp.data.bidProjOn.projNam);
                $(".projNbr span").html(resp.data.bidProjOn.projNbr);
                $(".bidDptAddr span").html(resp.data.bidProjOn.bidDptAddr);
                $(".bidOpenTim span").html((new Date(resp.data.bidProjOn.bidOpenTim)).Format("yyyy-MM-dd hh:mm"));
            }
        },
        error: function (err) {
            console.log(err);
        }
    });

// 获取供应商报价信息
    function getAllQotInfo(){
        jQuery.ajax({
            url: HOST_ADDR + '/cpmp/bidProjOnMngForPchs/getOpenRcds/' + projId,
            type: 'get',
            dataType: 'json',
            contentType: "application/json",
            headers: {
                'access_token': token, 'login_type': 'ums'
            },
            success: function (resp) {
                if (resp.success && resp.msgCod=='S0000') {
                    showTableData(resp.data);
                } else {
                    alert(resp.msgCod + ' ' + resp.msgInf);
                    return;
                }
            },
            error: function (err) {
                console.log(err);
            }
        });
    }
    getAllQotInfo();

    function showTableData(datalist){
        var colNames = ["物料编码","物料名称","单位","交货时间","交货地址"];
        var splrlists = [];
        var rows = [];   // 所有行数据
        var groupHeaders = [];
        var colModel = [
            {name:"matCod",index:"matCod",align:'left',width : 80,sortable : false},
            {name:"matNam",index:"matNam",align:'left',width : 120,sortable : false},
            {name:"matUnt",index:"matUnt",align:'left',width : 100,sortable : false},
            {name:"dlvDteStr",index:"dlvDteStr",align:'center',width : 120,sortable : false},
            {name:"dlvAdr",index:"dlvAdr",align:'center',width : 120,sortable : false}
        ];
        if(datalist.length){
            if(typeof(datalist[0].splrQotIoDtos) != 'undefined' && datalist[0].splrQotIoDtos.length){
                for(var i=0; i< datalist[0].splrQotIoDtos.length; i++){
                    splrlists.push(datalist[0].splrQotIoDtos[i].splrNam); // 参与报价的供应商列表,"单价","数量","币种"
                    groupHeaders.push({startColumnName:"price"+i,numberOfColumns:3,titleText:datalist[0].splrQotIoDtos[i].splrNam});
                    colNames.push("单价");
                    colNames.push("数量");
                    colNames.push("币种");
                    colModel.push({name:"price"+i,index:"price"+i,align:'center',width : 60,sortable : false});
                    colModel.push({name:"splNum"+i,index:"splNum"+i,align:'center',width : 60,sortable : false});
                    colModel.push({name:"currTyp"+i,index:"currTyp"+i,align:'center',width : 60,sortable : false,editable: false,edittype: "select",formatter: 'select',editoptions: {value: getCurrTyp()}});
                }
            }else{
                alert("数据异常！");
            }
        }

        for(var j=0;j<datalist.length;j++){
            var tmprow = {};
            tmprow.matCod = datalist[j].matCod;
            tmprow.matNam = datalist[j].matNam;
            tmprow.matUnt = datalist[j].matUnt ? datalist[j].matUnt : '';
            tmprow.dlvDteStr = datalist[j].dlvDte ? (new Date(datalist[j].dlvDte)).Format("yyyy-MM-dd") : '';
            tmprow.dlvAdr = datalist[j].dlvAdr ? datalist[j].dlvAdr : '';
            for(var k=0;k<datalist[j].splrQotIoDtos.length;k++){
                tmprow["price"+k] = datalist[j].splrQotIoDtos[k].price;
                tmprow["splNum"+k] = datalist[j].splrQotIoDtos[k].splNum;
                tmprow["currTyp"+k] = datalist[j].splrQotIoDtos[k].currTyp;
            }
            rows.push(tmprow);
        }
        console.log("**********1111111**************");
        console.log(rows);
        console.log("**********2222222**************");

        $("#mygrid").jqGrid({
            width:'100%',
            dataType:'local',
            colNames:colNames,
            colModel:colModel,
            viewrecords:true,
            rowNum:-1,
            fitColumns: true,
            shrinkToFit: true,
            altRows: true
        });
        $("#mygrid").jqGrid('setGroupHeaders', {
            useColSpanStyle: true,
            groupHeaders:groupHeaders
        });
        for ( var i = 0; i < rows.length; i++){
            $("#mygrid").jqGrid('addRowData', i + 1, rows[i]);
        }
        if($("#mygrid").width()< $(".qotinfo").width()-2){
            $("#mygrid").jqGrid('setGridWidth', $(".qotinfo").width()-2);
        }
    }

    $(window).resize(function(){
        if($("#mygrid").width()< $(".qotinfo").width()-2){
            $("#mygrid").jqGrid('setGridWidth', $(".qotinfo").width()-2);
        }
    });

// 获取币种汇率
    function getCurrTyp() {
        var parmTyps = 'CURR_TYP';
        var tmplist = {};
        $.ajax({
            url: HOST_ADDR + '/cpmp/comParm/getByParmTyp/' + parmTyps,
            type: 'get',
            dataType: 'json',
            async: false,
            headers: {'access_token': token, 'login_type': 'ums'},
            contentType: 'application/json',
            success: function (resp) {
                if (!resp.success) {
                    alert(resp.msgCod + " - " + resp.msgInf);
                    console.log(resp.msgCod + " - " + resp.msgInf);
                    return;
                }
                if (jQuery.isArray(resp.data)) {
                    for (var i = 0; i < resp.data.length; i++) {
                        currTyp[resp.data[i].parmCod] = resp.data[i].parmMemo;
                        tmplist[resp.data[i].parmCod] = resp.data[i].parmMemo;
                    }
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
        return tmplist;
    }
});


// 新接口调用
/**
 * 20171206
 */
/*
jQuery.ajax({
    url: HOST_ADDR + `/cpmp/bidProjOnMngForPchs/getAppAwdInf/` + projId,
    type: 'get',
    dataType: 'json',
    headers: {
        'access_token': token, 'login_type': 'ums'
    },
    contentType: "application/json",
    success: function (resp) {
        if (!resp.success) {
            console.log(resp.msgCod + ' ' + resp.msgInf);
            return;
        }
        console.log(resp);
        showTableData(resp.data.appAwdInfDtls);
    },
    error: function (err) {
        console.log(err);
    }
});

function showTableData(datalist){
    var colNames = ["物料编码","物料名称","单位","交货地址","交货时间","备注"];
    var splrlists = [];
    var rows = [];   // 所有行数据
    var groupHeaders = [];
    var colModel = [
        {name:"matCod",index:"matCod",align:'left',width : 80,sortable : false},
        {name:"matNam",index:"matNam",align:'left',width : 120,sortable : false},
        {name:"matUnt",index:"matUnt",align:'left',width : 60,sortable : false},
        {name:"dlvAdr",index:"dlvAdr",align:'left',width : 60,sortable : false},
        {name:"dlvDte",index:"dlvDte",align:'left',width : 60,sortable : false},
        {name:"matMemo",index:"matMemo",align:'left',width : 100,sortable : false}
    ];
    if(datalist.length){
        if(typeof(datalist[0].qotDtls) != 'undefined' && datalist[0].qotDtls.length){
            for(var i=0; i< datalist[0].qotDtls.length; i++){
                splrlists.push({"splrId":datalist[0].qotDtls[i].splrId,"name":datalist[0].qotDtls[i].fullNam}); // 参与报价的供应商列表,"单价","数量","币种"
                groupHeaders.push({startColumnName:"price"+i,numberOfColumns:3,titleText:datalist[0].qotDtls[i].fullNam});
                colNames.push("单价");
                colNames.push("数量");
                colNames.push("币种");
                colNames.push("应标交货日期");
                colModel.push({name:"price"+i,index:"price"+i,align:'center',width : 60,sortable : false});
                colModel.push({name:"splNum"+i,index:"splNum"+i,align:'center',width : 60,sortable : false});
                colModel.push({name:"currTyp"+i,index:"currTyp"+i,align:'center',width : 60,sortable : false,editable: false,edittype: "select",formatter: 'select',editoptions: {value: getCurrTyp()}});
                colModel.push({name:"tendDlvDteFmt"+i,index:"tendDlvDteFmt"+i,align:'center',width : 80,sortable : false});
            }
        }else{
            alert("数据异常！");
        }
    }

    for(var j=0;j<datalist.length;j++){
        var tmprow = {};
        tmprow.matCod = datalist[j].matDtl.matCod;
        tmprow.matNam = datalist[j].matDtl.matNam;
        tmprow.matUnt = datalist[j].matDtl.matUnt ? datalist[j].matDtl.matUnt : '';
        tmprow.matMemo = datalist[j].matDtl.memo ? datalist[j].matDtl.memo : '';
        tmprow.dlvAdr = datalist[j].matDtl.dlvAdr ? datalist[j].matDtl.dlvAdr : '';
        tmprow.dlvDteFmt = datalist[j].matDtl.dlvDte ? (new Date(datalist[j].matDtl.dlvDte)).Format("yyyy-MM-dd") : '';
        for(var k=0;k<splrlists.length;k++){
            for(var m=0;m<datalist[j].qotDtls.length;m++){
                if(splrlists[k].splrId==datalist[j].qotDtls[m].splrId){
                    tmprow["price"+k] = datalist[j].qotDtls[m].untPri;
                    tmprow["splNum"+k] = datalist[j].qotDtls[m].splNum;
                    tmprow["currTyp"+k] = datalist[j].qotDtls[m].currTyp ? datalist[j].qotDtls[m].currTyp : '';
                    tmprow["tendDlvDteFmt"+k] = datalist[j].qotDtls[m].tendDlvDte ? (new Date(datalist[j].qotDtls[m].tendDlvDte)).Format("yyyy-MM-dd") : '';
                }
            }
        }
        rows.push(tmprow);
    }

    $("#mygrid").jqGrid({
        width:'100%',
        dataType:'local',
        colNames:colNames,
        colModel:colModel,
        viewrecords:true,
        rowNum:-1,
        fitColumns: true,
        shrinkToFit: true,
        altRows: true
    });
    $("#mygrid").jqGrid('setGroupHeaders', {
        useColSpanStyle: true,
        groupHeaders:groupHeaders
    });
    for ( var i = 0; i < rows.length; i++){
        $("#mygrid").jqGrid('addRowData', i + 1, rows[i]);
    }
    if($("#mygrid").width()< $(".qotinfo").width()-2){
        $("#mygrid").jqGrid('setGridWidth', $(".qotinfo").width()-2);
    }

    // 获取币种汇率
    function getCurrTyp() {
        var parmTyps = 'CURR_TYP';
        var tmplist = {};
        $.ajax({
            url: HOST_ADDR + '/cpmp/comParm/getByParmTyp/' + parmTyps,
            type: 'get',
            dataType: 'json',
            async: false,
            headers: {'access_token': token, 'login_type': 'ums'},
            contentType: 'application/json',
            success: function (resp) {
                if (!resp.success) {
                    alert(resp.msgCod + " - " + resp.msgInf);
                    console.log(resp.msgCod + " - " + resp.msgInf);
                    return;
                }
                if (jQuery.isArray(resp.data)) {
                    for (var i = 0; i < resp.data.length; i++) {
                        tmplist[resp.data[i].parmCod] = resp.data[i].parmMemo;
                    }
                }
            },
            error: function (err) {
                console.log(err);
            }
        })
        return tmplist;
    }
}
*/
